package com.scmt.hmzd.serviceimpl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scmt.hmzd.entity.HmOrder;
import com.scmt.hmzd.enums.OrderEnum;
import com.scmt.hmzd.service.IHmOrderService;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.hmzd.mapper.HmOrderMapper;
import com.scmt.core.utis.FileUtil;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author
 **/
@Service
public class HmOrderServiceImpl extends ServiceImpl<HmOrderMapper, HmOrder> implements IHmOrderService {
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private HmOrderMapper hmOrderMapper;

    @Override
    public IPage<HmOrder> queryHmOrderListByPage(HmOrder hmOrder, SearchVo searchVo, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmOrder> pageData = new Page<>(page, limit);
        QueryWrapper<HmOrder> queryWrapper = new QueryWrapper<>();
        if (hmOrder != null) {
            queryWrapper = LikeAllField(hmOrder, searchVo);
        }
        IPage<HmOrder> result = hmOrderMapper.selectPage(pageData, queryWrapper);
        return result;
    }

    @Override
    public void download(HmOrder hmOrder, HttpServletResponse response) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<HmOrder> queryWrapper = new QueryWrapper<>();
        if (hmOrder != null) {
            queryWrapper = LikeAllField(hmOrder, null);
        }
        List<HmOrder> list = hmOrderMapper.selectList(queryWrapper);
        for (HmOrder re : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("订单金额", re.getOrderPrice());
            map.put("服务时长", re.getTimeLong());
            map.put("项目名称 多个以逗号分隔", re.getProjectName());
            map.put("技师姓名", re.getTechnicianName());
            map.put("下单时间", re.getCreateTime());
            map.put("服务时间（客户预约时间）", re.getServiceTime());
            map.put("车费费用", re.getCarMoney());
            map.put("车费详情", re.getCarInfo());
            map.put("支付状态 0未支付 1已支付 2技师接单 3服务完成 -1支付失败", re.getStatus());
            map.put("订单备注", re.getRemark());
            map.put("是否升级项目 1是0否", re.getIsUp());
            map.put("优惠金额", re.getDiscountPrice());
            map.put("收件人姓名", re.getReceiverName());
            map.put("收件人电话", re.getReceiverMobile());
            mapList.add(map);
        }
        FileUtil.createExcel(mapList, "exel.xlsx", response);
    }

    @Override
    public List<HmOrder> listUnRefundOrderAndOrderItems(int status, DateTime offsetMinute) {
        return hmOrderMapper.listUnRefundOrderAndOrderItems(status, offsetMinute);
    }

    @Override
    public void cancelOrders(List<HmOrder> orders) {
        hmOrderMapper.cancelOrders(orders);
    }

    @Override
    public Map<String, Object> statisticsOrderData() {
        return hmOrderMapper.statisticsOrderData();
    }

    @Override
    public List<Map<String, Object>> queryWeekData() {
        return hmOrderMapper.queryWeekData();
    }

    @Override
    public IPage<HmOrder> queryOrderListDataByPage(HmOrder hmOrder, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmOrder> pageData = new Page<>(page, limit);
        QueryWrapper<HmOrder> queryWrapper = new QueryWrapper<>();
        if (hmOrder != null) {
            queryWrapper = LikeAllField(hmOrder, null);
        }
        queryWrapper.lambda().ne(HmOrder::getStatus, OrderEnum.ORDER_CANCEL.getIndex());
        queryWrapper.orderByDesc("create_time");
        IPage<HmOrder> result = hmOrderMapper.queryOrderListDataByPage(queryWrapper,pageData);
        return result;
    }

    @Override
    public List<HmOrder> listUnCloseOrder() {
        return hmOrderMapper.listUnCloseOrder();
    }

    /**
     * 功能描述：构建模糊查询
     *
     * @param hmOrder 需要模糊查询的信息
     * @return 返回查询
     */
    public QueryWrapper<HmOrder> LikeAllField(HmOrder hmOrder, SearchVo searchVo) {
        QueryWrapper<HmOrder> queryWrapper = new QueryWrapper<>();
        if (hmOrder.getId() != null) {
            queryWrapper.and(i -> i.eq("hm_order.id", hmOrder.getId()));
        }

        if (StringUtils.isNotBlank(hmOrder.getProjectName())) {
            queryWrapper.and(i -> i.like("hm_order.project_name", hmOrder.getProjectName()));
        }
        if (hmOrder.getTechnicianId() != null) {
            queryWrapper.and(i -> i.eq("hm_order.technician_id", hmOrder.getTechnicianId()));
        }
        if (StringUtils.isNotBlank(hmOrder.getTechnicianName())) {
            queryWrapper.and(i -> i.like("hm_order.technician_name", hmOrder.getTechnicianName()));
        }
        if (StringUtils.isNotBlank(hmOrder.getUserId())) {
            queryWrapper.and(i -> i.eq("hm_order.user_id", hmOrder.getUserId()));
        }

        if (hmOrder.getStatus() != null) {
            queryWrapper.and(i -> i.eq("hm_order.status", hmOrder.getStatus()));
        }

        if (hmOrder.getAddrOrderId() != null) {
            queryWrapper.and(i -> i.eq("hm_order.addr_order_id", hmOrder.getAddrOrderId()));
        }

        if (hmOrder.getIsUp() != null) {
            queryWrapper.and(i -> i.eq("hm_order.is_up", hmOrder.getIsUp()));
        }
        if (hmOrder.getIsComment() != null) {
            queryWrapper.and(i -> i.eq("hm_order.is_comment", hmOrder.getIsComment()));
        }

        if (StringUtils.isNotBlank(hmOrder.getReceiverName())) {
            queryWrapper.and(i -> i.like("hm_order.receiver_name", hmOrder.getReceiverName()));
        }
        if (StringUtils.isNotBlank(hmOrder.getReceiverMobile())) {
            queryWrapper.and(i -> i.like("hm_order.receiver_mobile", hmOrder.getReceiverMobile()));
        }

        if (searchVo != null) {
            if (searchVo.getStartDate() != null && searchVo.getEndDate() != null) {
                queryWrapper.lambda().and(i -> i.between(HmOrder::getCreateTime, searchVo.getStartDate(), searchVo.getEndDate()));
            }
        }
        queryWrapper.and(i -> i.eq("hm_order.del_flag", 0));
//        queryWrapper.lambda().and(i -> i.eq(HmOrder::getDelFlag, 0));
        return queryWrapper;
    }
}
